/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package net.octopeer.core.config;

import java.io.File;

import java.util.ArrayList;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;


import net.octopeer.core.Peer;
import net.octopeer.core.PeerNode;
import net.octopeer.core.Resource;
import net.octopeer.utilities.XMLTools;

/**
 *
 * @author andria
 */
public class ResourceListConfig {
    public ResourceListConfig() {
        
    }
    
    public ArrayList<Resource> readResourceListConfig() throws Exception {
        ArrayList<Resource> resourceList = new ArrayList<Resource>();
        
        Document dom = null;
        File file = null;
        Peer result = null;
        XMLTools xt = new XMLTools();
        
        // Parse the XML stream input to Document type
        file = new File("./conf/resourceList.xml");
        dom = xt.db.parse(file);
        
        Element rootEle = dom.getDocumentElement();
        
        NodeList peerResourceListList = rootEle.getElementsByTagName("ResourceList");
        NodeList peerResourceList = rootEle.getElementsByTagName("ResourceList:Resource");

        int peerResourceLoop = 0;

        while (peerResourceLoop < peerResourceList.getLength()) {
            NodeList peerResourceListEle = peerResourceList.item(peerResourceLoop).getChildNodes();

            Resource peerResource = new Resource();

            String resourceIDText = peerResourceListEle.item(0).getFirstChild().getNodeValue();
            String resourceTypeText = peerResourceListEle.item(1).getFirstChild().getNodeValue();
            String resourceDetailText = peerResourceListEle.item(2).getFirstChild().getNodeValue();

            peerResource.setResourceID(resourceIDText);
            peerResource.setResourceType(resourceTypeText);
            peerResource.setResourceDetail(resourceDetailText);

            resourceList.add(peerResource);

            peerResourceLoop++;
        }
        
        return resourceList;
    }

    public void updateResourceListConfig(Peer peer) throws Exception {
        ArrayList<Resource> resourceList = peer.getResourceList();
        
        // Prepare the DOM document
        Document doc;
        XMLTools xt = new XMLTools();        
        doc = xt.db.newDocument();
        
        Element rootEle = doc.createElement("ResourceList");
        
        int resourceLoop = 0;
        while (resourceLoop < resourceList.size()) {
            Resource peerResource = (Resource) resourceList.get(resourceLoop);

            Element peerResourceEle = doc.createElement("ResourceList:Resource");
            // ID
            Element peerResourceIDEle = doc.createElement("ResourceList:ID");
            Text peerResourceIDText = doc.createTextNode(peerResource.getResourceID());
            peerResourceIDEle.appendChild(peerResourceIDText);
            peerResourceEle.appendChild(peerResourceIDEle);        

            // Type
            Element peerResourceTypeEle = doc.createElement("ResourceList:Type");
            Text peerResourceTypeText = doc.createTextNode(peerResource.getResourceType());
            peerResourceTypeEle.appendChild(peerResourceTypeText);
            peerResourceEle.appendChild(peerResourceTypeEle);        

            // Detail
            Element peerResourceDetailEle = doc.createElement("ResourceList:Detail");
            Text peerResourceDetailText = doc.createTextNode(peerResource.getResourceDetail());
            peerResourceDetailEle.appendChild(peerResourceDetailText);
            peerResourceEle.appendChild(peerResourceDetailEle);  

            rootEle.appendChild(peerResourceEle);
            
            resourceLoop++;
        }
        
        doc.appendChild(rootEle);      
        
        // Prepare the DOM document for writing
        Source source = new DOMSource(doc);

        // Prepare the output file
        File file = new File("./conf/resourceList.xml");
        Result result = new StreamResult(file);

        // Write the DOM document to the file
        Transformer xformer = TransformerFactory.newInstance().newTransformer();
        xformer.transform(source, result);        
    }
}
